package org.com.yz.dubbo.web.ctrl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringEscapeUtils;
import org.com.yz.common.annotion.Parameter;
import org.com.yz.common.constant.PayConstant;
import org.com.yz.common.util.ClassRefUtil;
import org.com.yz.dal.dao.model.BalanceQuery;
import org.com.yz.dal.dao.model.BalanceQueryEs;
import org.com.yz.dal.dao.model.MchConfig;
import org.com.yz.dubbo.api.service.IBalanceQueryService;
import org.com.yz.dubbo.api.service.IMchConfigService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 余额查询控制类
 *
 * @ClassName BalanceQueryCtr
 * @Author wzc
 * @Date 2018/8/6 14:04
 **/
@RestController
public class BalanceQueryCtr {


    @Reference(version = "1.0.0", timeout = 30000, retries = 0)
    private IBalanceQueryService iBalanceQueryService;

    @Reference(version = "1.0.0", timeout = 80000, retries = 0)
    private IMchConfigService iMchConfigService;


    /**
     * @param data, sign
     * @Description: 余额查询
     * @author wzc
     * @date 2018/8/6 14:16
     */
    @PostMapping(value = "/api/balanceQuery.htm")
    @Parameter
    public String balanceQuery(String data, String sign) {
        BalanceQueryEs balanceQueryEs = JSONObject.parseObject(StringEscapeUtils.unescapeJava(data), BalanceQueryEs.class);
        BalanceQueryEs res = new BalanceQueryEs();
        try {
            MchConfig mchConfig = new MchConfig();
            mchConfig.setAcctid(balanceQueryEs.getBizContent().getAcctid());
            mchConfig.setMchNo(balanceQueryEs.getMchNo());
            List<MchConfig> mchConfigList = iMchConfigService.listCongi(mchConfig);    //查询该商户的付款信息
            if (mchConfigList.size() <= 0) {
                res.setBizContent(null);
                return ClassRefUtil.createRetData(balanceQueryEs, res, PayConstant.RETURN_DEPOSIT_BALANCE_FAIL, PayConstant.RETURN_DEPOSIT_BALANCE_FAILS);

            }
            BalanceQuery balanceQuery = iBalanceQueryService.balanceQueryHandle(mchConfigList.get(0), balanceQueryEs.getOutTransNo());
            if (balanceQuery != null) {
                res.setBizContent(balanceQuery);
                return ClassRefUtil.createRetData(balanceQueryEs, res, PayConstant.RETURN_DEPOSIT_SUCESS, PayConstant.RETURN_ALIPAY_VALUE_SUCCESS);
            }
        } catch (Exception e) {
            res.setBizContent(null);
            return ClassRefUtil.createRetData(balanceQueryEs, res, PayConstant.RETURN_DEPOSIT_BANK_FAIL, PayConstant.RETURN_DEPOSIT_BANK_FAILS);
        }
        res.setBizContent(null);
        return ClassRefUtil.createRetData(balanceQueryEs, res, PayConstant.RETURN_DEPOSIT_CODE_ERROR, PayConstant.RETURN_DEPOSIT_CODES_ERROR);
    }


}
